﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using JumbotPay.API.YunGouOS.WxPay;
namespace JumbotPay.WebFile.YungGouOS_Wxpay_Mobile
{
    public partial class _return_url : JumbotPay.UI.BasicPage
    {
        public string account_wxpay = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            JumbotPay.Utils.Logs.Info("wxpayM_return", "" + Request.Url, false);

            account_wxpay = q("account_no");//可以获取

            WxPayConfig conf = new WxPayConfig(account_wxpay);
            WxPayData returnData = new WxPayData();
            returnData.SetValue("code", q("code"), true);
            returnData.SetValue("orderNo", q("orderNo"), true);
            returnData.SetValue("outTradeNo", q("outTradeNo"), true);
            returnData.SetValue("payNo", q("payNo"), true);
            returnData.SetValue("money", q("money"), true);
            returnData.SetValue("mchId", q("mchId"), true);
            returnData.SetValue("payChannel", q("payChannel"), false);
            returnData.SetValue("time", q("time"), false);
            returnData.SetValue("attach", q("attach"), false);
            returnData.SetValue("openId", q("openId"), false);



            returnData.SetValue("sign", q("sign"), false);

            JumbotPay.Utils.Logs.Info("wxpayM_retur", returnData.ToJson(), false);

            if (returnData.CheckSign_Must(conf.KEY))
            {
                if (returnData.GetValue("code").ToString() == "1")//支付通知
                {
                    string _out_trade_no = returnData.GetValue("outTradeNo").ToString();
                    string _order_id = returnData.GetValue("attach").ToString();
                    object[] value = new JumbotPay.DAL.OrderDAL().GetOrderFields("yungouos_wxpay", _order_id, _out_trade_no, "appid,out_trade_no,pay_status");
                    string appid = value[0].ToString();
                    _out_trade_no = value[1].ToString();
                    string _pay_status = value[2].ToString();

                    doh.Reset();
                    doh.ConditionExpress = "appid=@appid";
                    doh.AddConditionParameter("@appid", appid);
                    object[] value2 = doh.GetFields("jpay_app", "return_url");
                    if (value2 != null)
                    {
                        string _return_url = value2[0].ToString();
                        string key = new JumbotPay.DAL.AppDAL().GetKey(appid);
                        string[] array1 = { "order_id=" + _order_id, "pay_status=" + (_pay_status == "1" ? "0" : "-1"), "pay_order_id=" + _out_trade_no };
                        string sign1 = JumbotPay.Utils.Strings.GetSign(array1, key);
                        Response.Redirect(_return_url + "?" + JumbotPay.Utils.Strings.GetParaStr(array1, sign1));
                    }
                    else
                    {
                        Response.Write("app无效");
                    }
                }
                else
                {
                    Response.Write("签名验证失败");
                    Response.End();
                }
            }
            else
            {
                Response.Write("签名验证失败");
                Response.End();
            }


        }
    }
}